﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xpedite.Resources.Compressors;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources.Compressors
{
  [TestClass]
  public class AcceptEncodingHeaderTest
  {
    [TestMethod]
    public void TestCtor()
    {
      var header = new AcceptEncodingHeader("header", new Dictionary<String, AcceptEncoding> { { "coding", new AcceptEncoding("coding", 1) } });

      AssertAcceptEncodingHeader(header, "header", new AcceptEncoding("coding", 1));
    }

    [TestMethod]
    public void TestParse()
    {
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse(" "), " ");
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse(String.Empty), String.Empty);
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse("gzip"), "gzip", new AcceptEncoding("gzip", 1));
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse("gzip,deflate"), "gzip,deflate", new AcceptEncoding("gzip", 1), new AcceptEncoding("deflate", 1));
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse("gzip, deflate, "), "gzip, deflate, ", new AcceptEncoding("gzip", 1), new AcceptEncoding("deflate", 1));
      AssertAcceptEncodingHeader(AcceptEncodingHeader.Parse("gzip;q=0, deflate;q=1"), "gzip;q=0, deflate;q=1", new AcceptEncoding("gzip", 0), new AcceptEncoding("deflate", 1));

      Expect.Exception(() => AcceptEncodingHeader.Parse(null), new ArgumentNullException("header", "Value cannot be null."));
      Expect.Exception(() => AcceptEncodingHeader.Parse("deflate;q=1,deflate;q=0"), new ArgumentException("An item with the same key has already been added."));
    }

    [TestMethod]
    public void TestGetEncodingQualityFor()
    {
      Assert.AreEqual(0F, AcceptEncodingHeader.Parse(" ").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(0F, AcceptEncodingHeader.Parse(String.Empty).GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(1F, AcceptEncodingHeader.Parse("deflate;q=1").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(1F, AcceptEncodingHeader.Parse("deflate;q=1.0").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(1F, AcceptEncodingHeader.Parse("deflate;q=1.01").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(1F, AcceptEncodingHeader.Parse("gzip,deflate;q=1").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(0F, AcceptEncodingHeader.Parse("gzip,*;q=0").GetAcceptEncodingQualityFor("deflate"));
      Assert.AreEqual(0.5F, AcceptEncodingHeader.Parse("gzip,*;q=0.5").GetAcceptEncodingQualityFor("deflate"));
    }

    private static void AssertAcceptEncodingHeader(AcceptEncodingHeader actual, String expectedHeader, params AcceptEncoding[] expectedCodings)
    {
      Assert.AreEqual(expectedHeader, actual.Header);
      Assert.AreEqual(expectedCodings.Length, actual.Codings.Count());

      IList<AcceptEncoding> actualCodings = actual.Codings.ToArray();
      for (var i = 0; i < actualCodings.Count; i++)
      {
        Assert.AreEqual(expectedCodings[i].Coding, actualCodings[i].Coding);
        Assert.AreEqual(expectedCodings[i].Q, actualCodings[i].Q);
      }
    }
  }
}
